<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>WebSocket Push Test</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
  systemtype: <input id="demo" type="text" name="firstname"><button onclick="sendToServer()">send to server</button>
  <br>
  <div class="content"></div>

  <script>
    var wsuri = "ws://127.0.0.1:12345/ws";
    var token = "aaa";
    var e = "topic1";

    var insert = "<p>Start connecting " + wsuri + "</p>";
    document.getElementsByClassName("content")[0].innerHTML += insert;

    var sock = new WebSocket(wsuri);
    sock.onopen = function () {
      console.log("connected to " + wsuri);
      var insert = "<p>Connected to " + wsuri + "</p>";
      document.getElementsByClassName("content")[0].innerHTML += insert;

      sock.send("{\"token\": \"" + token + "\", \"event\": \"" + e + "\"}")
      insert = "<p>Register message sent</p>";
      document.getElementsByClassName("content")[0].innerHTML += insert;
    };
    sock.onclose = function (e) {
      console.log("connection closed (" + e.code + ")");
      var insert = "<p>Connection be closed</p>";
      document.getElementsByClassName("content")[0].innerHTML += insert;
    };
    sock.onmessage = function (e) {
      console.log("Receive message: " + e.data);

      var insert = "<p>Receive: " + e.data + "</p>";
      document.getElementsByClassName("content")[0].innerHTML += insert;
    };

    function sendToServer() {
      var x = document.getElementById("demo").value;
      sock.send("{\"token\": \"" + token + "\", \"event\": \"" + e + "\",\"body\":{\"systemType\": \"" + x + "\"}}")
    };
  </script>
</body>

</html>